package com.tang.managementdemo.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tang.managementdemo.mapper.EmpExprMapper;
import com.tang.managementdemo.mapper.EmpMapper;
import com.tang.managementdemo.pojo.*;
import com.tang.managementdemo.service.EmpLogService;
import com.tang.managementdemo.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private  EmpExprMapper empExprMapper;
    @Autowired private EmpLogService empLogService;
    @Override
    public List<Employ> findAll() {
        return empMapper.findAll();
    }

    @Override
    public PageResult page(EmpQueryParam empQueryParam) {
/*        //1.获取总记录数
        Long total =empMapper.count();

        //2.获取结果列表
        Integer start=(page-1)*pageSize;
        List<Employ> list = empMapper.page(start,pageSize);

        //3.封装结果
        return new PageResult(total,list);*/


        //1.设置分页参数
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());

        //2.执行查询
        List<Employ> list = empMapper.list(empQueryParam);
        Page<Employ> p= (Page<Employ>) list;

        //3.封装结果
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void save(Employ employ) {

        try{


            if(employ.getId()==null){
                //新增
                employ.setCreateTime(LocalDateTime.now());
                employ.setUpdateTime(LocalDateTime.now());
                empMapper.insert(employ);

//                int i=1/0;
                Integer empId = employ.getId();
                List<EmpExpr> exprList = employ.getExprList();
                if(!CollectionUtils.isEmpty(exprList)){
                    exprList.forEach(empExpr -> empExpr.setEmpId(empId));
                    empExprMapper.insertBatch(exprList);
                }
            }else{
            employ.setUpdateTime(LocalDateTime.now());
                //修改
            empMapper.update(employ);
            //删除信息
            empExprMapper.deleteById(Collections.singletonList(employ.getId()));
            //添加信息
                Integer empId = employ.getId();
                List<EmpExpr> exprList = employ.getExprList();
                if(!CollectionUtils.isEmpty(exprList)) {
                    exprList.forEach(empExpr -> empExpr.setEmpId(empId));
                    empExprMapper.insertBatch(exprList);
                }
            }

        } finally {
            //记录日志
            EmpLog emplog = new EmpLog(null, LocalDateTime.now(),employ.toString());
            empLogService.insertLog(emplog);
        }

    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void deleteById(List<Integer> ids) {
        try {
            empMapper.deleteById(ids);
            empExprMapper.deleteById(ids);
        }finally {
            //记录日志
            EmpLog emplog = new EmpLog(null, LocalDateTime.now(),"删除了用户"+ids.toString());
            empLogService.insertLog(emplog);
        }
    }

    @Override
    public Employ getById(Integer id) {
       Employ employ = empMapper.getById(id);
       employ.setExprList(empExprMapper.getByEmpId(id));
       return employ;
    }

}
